Writes one line of text to the emulator's log, so you can see what your game is doing while you test it. To print a value, write it after the text: log_write("hp is", hp) writes "hp is 5". You can add as many values as you like.
Works differently on Game Boy - The Game Boy has no log to write to, so log_write does nothing there.
Works differently on Game Boy Color - The Game Boy Color has no log to write to, so log_write does nothing there.
On other consoles this differs: see Side by side and How the consoles differ.
log_write(text, ...values)| Name | Type |
|---|---|
text | string |
...values | any (any number of them, none included) |
Nothing.
Writes one line of text to the emulator's log: a list of messages that grows beside the game while you test it. The player never sees it. It is how you look inside your game while it runs: what a variable is right now, whether an event ran at all, which frame something happened on.
log_write takes a text, and then as many values as you like. Each value is turned into words and put on the same line after the text, with a space before it. It works for a whole number, a real number, true or false, and another text.
var hp = 7;
log_write("hp is", hp);The log gets the line hp is 7. The number is worked out when the game runs, so it is always the value hp has at that moment, never a number you typed. Add more values the same way: log_write("hero", x, y, hp) writes hero 120 80 7.
if (btn_pressed(BTN_A)) {
log_write($"A pressed at x={x} y={y} on frame {game_frame()}");
}One line for every press of A, and nothing the rest of the time. A line that is written every frame fills the log fast. Put a log_write inside an if that checks for the thing you are curious about.
When the number belongs in the middle of a sentence, put it inside the text with $"..." instead: log_write($"hp is {hp} of {hp_max}"). Whatever is in curly brackets is turned into words for you, with the same kinds as above. The two ways make the same line.
A value that is not a number, true or false, or a text - a whole list, say - cannot be written, and the check says so. Write one thing from it instead, like items[0].
A real number is written with five numbers after the point, so 1.5 shows as 1.50000. A very long text is cut into several lines of 200 letters or fewer, always between two words, so no word is split. An empty text, log_write(""), writes nothing.
You read the log in the mGBA emulator, the one Gbabel plays your game in.
log_write adds one line.Your lines are the ones marked GBA Debug. mGBA writes its own notes there too, about the console's insides, and they look different, so start your message with a word you can search for, like "enemy:".
You can also read the log with no window at all. This runs the game for you and saves everything it writes into a file:
mgba-sdl.exe -l 255 game.gba > log.txtOpen log.txt and look for the lines that start with GBA Debug:. The same line shows up there as GBA Debug: hp is 7. A game built with --profile=log writes its frame report to the same place (see The frame profiler).
On a real Game Boy Advance, nothing is shown. The console has no log. A game on a cartridge cannot be read this way, and that is fine: log_write is for testing.
A build made with --release does nothing at all for this call. Not one line is written, and a plain text such as log_write("hello") leaves no trace in the game: the message and the call are both gone. There is one thing to know. The pieces of a $"..." text are built before log_write is called, and a release build can keep some of that work. It costs a few hundred bytes, not a line in any log. So log with $"..." while you test, and take the calls out when the game is finished.
These sizes were measured on a small game that had only a Step event, with and without the calls:
| What the game calls | Development build | --release build |
|---|---|---|
| nothing (the starting point) | 190,668 bytes | 148,316 bytes |
log_write("hello") once | +336 bytes | +0 bytes |
| three calls that put numbers in their text | +1,488 bytes | +720 bytes |
A game that never calls log_write pays nothing. Its ROM is exactly the same size as before the call existed.
In a development build each call also costs a little time, because the line has to be put together and handed to the emulator. A call that runs on every frame, in several objects, adds up. That is another reason to log only when something happens.
It takes a text, and then any number of values, which are put on the same line after the text, each with a space before it: log_write("hp is", hp, "of", hp_max) writes "hp is 5 of 10". A value can be a whole number, a real number (shown with five numbers after the point), true or false, or a string - the same kinds a $"..." text can hold, so you can also put values inside the text yourself: log_write($"hp is {hp}") does the same job. Read the line in mGBA: choose Tools, then View logs..., and look for the "GBA Debug" lines (or run mgba-sdl.exe -l 255 game.gba from a command window). A real console has no log, so it shows nothing there. A game built with --release does nothing at all for this call: the line is never written, and a plain text such as log_write("hello") leaves nothing in the game. The pieces of a $"..." text are built before the call, so a release build can keep a few hundred bytes of them. A text longer than 200 characters is written as several lines, and an empty text writes nothing.